package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.DevGroup;
import com.ruoyi.system.domain.DeviceType;
import com.ruoyi.system.domain.SysUsers;
import com.ruoyi.system.service.DevGroupService;
import com.ruoyi.system.service.DeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TrigerList;
import com.ruoyi.system.service.ITrigerListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 触发器列表Controller
 * 
 * @author zbm
 * @date 2023-06-28
 */
@RestController
@RequestMapping("/system/list")
@Api("触发器相关")
public class TrigerListController extends BaseController
{
    @Autowired
    private ITrigerListService trigerListService;
    @Autowired
    private DevGroupService devGroupService;

    /**
     * 查询触发器列表列表
     */
    @ApiOperation("查询触发器列表")
    @PreAuthorize("@ss.hasPermi('system:list:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrigerList trigerList)
    {
        startPage();
        List<TrigerList> list = trigerListService.selectTrigerListList(trigerList);
        return getDataTable(list);
    }
    @ApiOperation("修改触发器状态")
    @PutMapping("/changeListStatus")
    public AjaxResult updateStatus(@RequestBody TrigerList trigerList){
        return toAjax(trigerListService.updateTrigerList(trigerList));
    }

    /**
     * 导出触发器列表列表
     */
    @ApiOperation("导出触发器列表")
    @PreAuthorize("@ss.hasPermi('system:list:export')")
    @Log(title = "触发器列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrigerList trigerList)
    {
        List<TrigerList> list = trigerListService.selectTrigerListList(trigerList);
        ExcelUtil<TrigerList> util = new ExcelUtil<TrigerList>(TrigerList.class);
        System.out.println();
        util.exportExcel(response, list, "触发器列表数据");
    }

    /**
     * 获取触发器列表详细信息
     */
    @ApiOperation("获取触发器列表详细信息")
    @PreAuthorize("@ss.hasPermi('system:list:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(trigerListService.selectTrigerListById(id));
    }

    /**
     * 新增触发器列表
     */
    @ApiOperation("新增触发器列表")
    @PreAuthorize("@ss.hasPermi('system:list:add')")
    @Log(title = "新增触发器列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrigerList trigerList) {
        List<TrigerList> trigerLists = trigerListService.selectTrigerList();

        TrigerList checkList = new TrigerList();
        checkList.setName(trigerList.getName());
        checkList.setEquipmentType(trigerList.getEquipmentType());
        checkList.setRegion(trigerList.getRegion());
        checkList.setDataType(trigerList.getDataType());
        checkList.setCondition(trigerList.getCondition());
        checkList.setParameter(trigerList.getParameter());
        boolean res=trigerLists.contains(checkList);
        if (!res){
            return toAjax(trigerListService.insertTrigerList(trigerList));
        }else {
            return AjaxResult.error("触发器重复");
        }
    }

    /**
     * 修改触发器列表
     */
    @ApiOperation("修改触发器列表")
    @PreAuthorize("@ss.hasPermi('system:list:edit')")
    @Log(title = "修改触发器列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrigerList trigerList)
    {
        trigerListService.selectTrigerList();
        return toAjax(trigerListService.updateTrigerList(trigerList));
    }

    /**
     * 删除触发器列表
     */
    @ApiOperation("删除触发器列表")
    @PreAuthorize("@ss.hasPermi('system:list:remove')")
    @Log(title = "删除触发器列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(trigerListService.deleteTrigerListByIds(ids));
    }

    /**
     * 查询设备类型表的设备名称
     * @param
     * @return
     */
    @ApiOperation("查询设备类型表的设备名称")
    @PreAuthorize("@ss.hasPermi('system:manage:listDeviceName')")
    @GetMapping("/listDeviceName")
    public TableDataInfo listDeviceName(DevGroup devGroup){
        List<DevGroup> list = devGroupService.ListDevType(devGroup);
        return getDataTable(list);
    }
}
